import org.apache.http.HttpException;
import DTO.userinfomation;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
@WebServlet("/login_servlet")
public class login_servlet extends HttpServlet {
    private static final String URL = "jdbc:h2:~/jpa-demo-db;MODE=MYSQL;DB_CLOSE_DELAY=-1";
    private static final String NAME = "sa";
    private static final String PASSWORD = "sa";
    public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException {
        doGet(req,resp);
    }
    public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        int flag=0,flag1 = 0;
        try {
            Connection conn = DriverManager.getConnection(URL,NAME,PASSWORD);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select  * from USERINFOMATION");
            while(rs.next()){
                if((rs.getString("USERNAME").equals(username)) && (!rs.getString("PASSWORD").equals(password))){
                    flag1 = 1;
                    break;
                }
                if(rs.getString("USERNAME").equals(username)&&rs.getString("PASSWORD").equals(password)){
                    flag = 1;
                    HttpSession session=req.getSession(true);
                    Map<String,String > map = new HashMap<>();
                    map.put("username",rs.getString("USERNAME"));
                    map.put("phone",rs.getString("PHONE"));
                    map.put("password",rs.getString("PASSWORD"));
                    map.put("img",rs.getString("IMG"));
                    session.setAttribute("USER",map);
                }
                if(flag == 1) break;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            resp.sendRedirect("error.html");
        }
        if(flag == 1)  resp.sendRedirect("user.jsp");
        else if(flag == 0 && flag1 == 0) {
            resp.setContentType("text/html;charset=UTF-8");
            resp.getWriter().println("没有此用户,请您注册后再登录!");
            resp.getWriter().println("<br>");
            resp.getWriter().println("<input type = submit value = 点此重新登录 onclick = location='login1.jsp'>");
        }
        else if(flag1 == 1&& flag == 0){
            resp.setContentType("text/html;charset=UTF-8");
            resp.getWriter().println("您输入的密码有误,请检查后登录!");
            resp.getWriter().println("<br>");
            resp.getWriter().println("<input type = submit value = 点此重新登录 onclick = location='login1.jsp'>");
        }
        else resp.sendRedirect("error.html");
    }
}
